package com.cloudsigns.factorymanager2.engineering.model;

import android.text.TextUtils;

import com.cloudsigns.factorymanager2.MyApplicaton;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringDailyFeedBackBean;
import com.cloudsigns.factorymanager2.engineering.bean.UserInfoBean;
import com.cloudsigns.factorymanager2.engineering.utils.EngineeringUtil;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.ProjectUtil;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tony on 2018/6/17.
 */

public class FeedBackDetailModel implements IFeedBackDetailModel {


    @Override
    public void feedBackDetail(final String dailyFeedbackId, final String id, final CommonEnum.EngineeringORderCategory type, final OnFeedBackDetailListener feedBackDetailListener) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String orderTypeStr = "";
                if (null != type) {
                    if (type == CommonEnum.EngineeringORderCategory.INSTALL)
                        orderTypeStr = "INSTALL";
                    if (type == CommonEnum.EngineeringORderCategory.BATCH_INSTALL)
                        orderTypeStr = "BATCH_INSTALL";
                    if (type == CommonEnum.EngineeringORderCategory.CONSTRUCT)
                        orderTypeStr = "CONSTRUCT";
                    if (type == CommonEnum.EngineeringORderCategory.MAINTAIN)
                        orderTypeStr = "MAINTAIN";
                    if (type == CommonEnum.EngineeringORderCategory.PUBLICATION)
                        orderTypeStr = "PUBLICATION";
                    if (type == CommonEnum.EngineeringORderCategory.PATROL)
                        orderTypeStr = "PATROL";
                }

                try {

                    String uri = ProjectUtil.FEEBBACK_DETAIL + "?dailyFeedbackId=" + dailyFeedbackId + "&id=" + id + "&type=" + orderTypeStr;
                    HttpClient client = new DefaultHttpClient();
                    HttpGet httpGet = new HttpGet(uri);

                    HttpResponse response;

                    httpGet.addHeader("token", MyApplicaton.getApplication().getToken());
                    httpGet.addHeader(HTTP.CONTENT_TYPE, ConstantUtil.CONTENT_TYPE);

                    response = client.execute(httpGet);
                    String result = EntityUtils.toString(response.getEntity(), "UTF-8").trim();
                    JSONObject json = new JSONObject(result);
                    String code = json.getString("code");
                    if (null != feedBackDetailListener) {
                        if (code.equals(EngineeringUtil.HTTP_SUCCESSED_COD)) {
                            JSONObject engineeringDailyFeedbackListJsonObj = new JSONObject(json.getString("engineeringDailyFeedback"));

                            EngineeringDailyFeedBackBean engineeringDailyFeedBackBean = new EngineeringDailyFeedBackBean();

                            if (engineeringDailyFeedbackListJsonObj != null) {

                                engineeringDailyFeedBackBean.setAccountId(engineeringDailyFeedbackListJsonObj.getString("accountId"));
                                engineeringDailyFeedBackBean.setAccountName(engineeringDailyFeedbackListJsonObj.getString("accountName"));
                                engineeringDailyFeedBackBean.setAdvice(engineeringDailyFeedbackListJsonObj.getString("advice"));
                                engineeringDailyFeedBackBean.setCompleteUnusualAccountId(engineeringDailyFeedbackListJsonObj.getString("completeUnusualAccountId"));
                                engineeringDailyFeedBackBean.setConfirmCompleteUnusualAccountName(engineeringDailyFeedbackListJsonObj.getString("completeUnusualAccountName"));
                                String completeUnusualDateStr = engineeringDailyFeedbackListJsonObj.getString("completeUnusualDate");
                                if (!TextUtils.isEmpty(completeUnusualDateStr) && !completeUnusualDateStr.equals("null")) {
                                    engineeringDailyFeedBackBean.setCompleteUnusualDate(Long.parseLong(completeUnusualDateStr));
                                }
                                engineeringDailyFeedBackBean.setCompleteUnusualDesc(engineeringDailyFeedbackListJsonObj.getString("completeUnusualDesc"));
                                engineeringDailyFeedBackBean.setConfirmCompleteUnusualAccountId(engineeringDailyFeedbackListJsonObj.getString("confirmCompleteUnusualAccountId"));
                                String confirmCompleteUnusualDateStr = engineeringDailyFeedbackListJsonObj.getString("confirmCompleteUnusualDate");
                                if (!TextUtils.isEmpty(confirmCompleteUnusualDateStr) && !confirmCompleteUnusualDateStr.equals("null")) {
                                    engineeringDailyFeedBackBean.setConfirmCompleteUnusualDate(Long.parseLong(confirmCompleteUnusualDateStr));
                                }
                                engineeringDailyFeedBackBean.setContentDesc(engineeringDailyFeedbackListJsonObj.getString("contentDesc"));

                                String damageNumberStr = engineeringDailyFeedbackListJsonObj.getString("damageNumber");
                                if (null != damageNumberStr && !TextUtils.isEmpty(damageNumberStr) && !damageNumberStr.equals("null")) {
                                    engineeringDailyFeedBackBean.setDamageNumber(Integer.parseInt(damageNumberStr));
                                }


                                engineeringDailyFeedBackBean.setDamageProduct(engineeringDailyFeedbackListJsonObj.getString("damageProduct"));

                                String delayDaysStr = engineeringDailyFeedbackListJsonObj.getString("delayDays");
                                if (null != delayDaysStr && !TextUtils.isEmpty(delayDaysStr) && !delayDaysStr.equals("null")) {
                                    engineeringDailyFeedBackBean.setDelayDays(Integer.parseInt(delayDaysStr));
                                }

                                String feedbackDateStr = engineeringDailyFeedbackListJsonObj.getString("feedbackDate");
                                if (!TextUtils.isEmpty(feedbackDateStr) && !feedbackDateStr.equals("null")) {
                                    engineeringDailyFeedBackBean.setFeedbackDate(Long.parseLong(feedbackDateStr));
                                }

                                String handleDate = engineeringDailyFeedbackListJsonObj.getString("handleDate");
                                if (!TextUtils.isEmpty(handleDate) && !handleDate.equals("null")) {
                                    engineeringDailyFeedBackBean.setHandleDate(Long.parseLong(handleDate));
                                }
                                engineeringDailyFeedBackBean.setHandleInfo(engineeringDailyFeedbackListJsonObj.getString("handleInfo"));
                                engineeringDailyFeedBackBean.setHandlerAccountId(engineeringDailyFeedbackListJsonObj.getString("handlerAccountId"));
                                engineeringDailyFeedBackBean.setHandlerAccountName(engineeringDailyFeedbackListJsonObj.getString("handlerAccountName"));
                                engineeringDailyFeedBackBean.setId(engineeringDailyFeedbackListJsonObj.getString("id"));

                                String imgUrlListStr = engineeringDailyFeedbackListJsonObj.getString("imgUrlList");
                                if (null != imgUrlListStr && !"null".equals(imgUrlListStr) && !TextUtils.isEmpty(imgUrlListStr)) {
                                    JSONArray imgUrlListJsonArr = new JSONArray(imgUrlListStr);
                                    if (null != imgUrlListJsonArr) {
                                        List<String> imgUrlList = new ArrayList<String>();
                                        for (int j = 0; j < imgUrlListJsonArr.length(); j++) {
                                            String str = (String) imgUrlListJsonArr.get(j);
                                            imgUrlList.add(str);
                                        }
                                        engineeringDailyFeedBackBean.setImgUrlList(imgUrlList);
                                    }

                                }

                                engineeringDailyFeedBackBean.setItemId(engineeringDailyFeedbackListJsonObj.getString("itemId"));
                                engineeringDailyFeedBackBean.setOmitContent(engineeringDailyFeedbackListJsonObj.getString("omitContent"));


                                String omitNumberStr = engineeringDailyFeedbackListJsonObj.getString("omitNumber");
                                if (null != omitNumberStr && !TextUtils.isEmpty(omitNumberStr) && !omitNumberStr.equals("null")) {
                                    engineeringDailyFeedBackBean.setOmitNumber(Integer.parseInt(omitNumberStr));
                                }

                                String otherImgUrlListStr = engineeringDailyFeedbackListJsonObj.getString("otherImgUrlList");
                                if (null != otherImgUrlListStr && !TextUtils.isEmpty(otherImgUrlListStr) && !otherImgUrlListStr.equals("null")) {
                                    JSONArray otherImgUrlListJsonArr = new JSONArray(otherImgUrlListStr);
                                    if (null != otherImgUrlListJsonArr) {
                                        List<String> imgUrlList = new ArrayList<String>();
                                        for (int j = 0; j < otherImgUrlListJsonArr.length(); j++) {
                                            String str = (String) otherImgUrlListJsonArr.get(j);
                                            imgUrlList.add(str);
                                        }
                                        engineeringDailyFeedBackBean.setOtherImgUrlList(imgUrlList);
                                    }
                                }


                                engineeringDailyFeedBackBean.setStatus(engineeringDailyFeedbackListJsonObj.getString("status"));
                                String subTypeStr = engineeringDailyFeedbackListJsonObj.getString("subType");
                                if (!TextUtils.isEmpty(subTypeStr)) {
                                    if (subTypeStr.equals("DAMAGE")) {
                                        engineeringDailyFeedBackBean.setSubType(CommonEnum.SubType.DAMAGE);
                                    } else if (subTypeStr.equals("DELAY")) {
                                        engineeringDailyFeedBackBean.setSubType(CommonEnum.SubType.DELAY);
                                    } else if (subTypeStr.equals("INSTALL_UNUSUAL")) {
                                        engineeringDailyFeedBackBean.setSubType(CommonEnum.SubType.INSTALL_UNUSUAL);
                                    } else if (subTypeStr.equals("OMIT")) {
                                        engineeringDailyFeedBackBean.setSubType(CommonEnum.SubType.OMIT);
                                    }

                                }
                                String typeStr = engineeringDailyFeedbackListJsonObj.getString("type");
                                if (!TextUtils.isEmpty(typeStr)) {
                                    if (typeStr.equals("NORMAL")) {
                                        engineeringDailyFeedBackBean.setType(CommonEnum.FeedBackType.NORMAL);
                                    } else if (typeStr.equals("UNUSUAL")) {
                                        engineeringDailyFeedBackBean.setType(CommonEnum.FeedBackType.UNUSUAL);
                                    } else if (typeStr.equals("COMPLETED")) {
                                        engineeringDailyFeedBackBean.setType(CommonEnum.FeedBackType.COMPLETED);
                                    }

                                }


                                String unusualDaysStr = engineeringDailyFeedbackListJsonObj.getString("unusualDays");
                                if (null != unusualDaysStr && !TextUtils.isEmpty(unusualDaysStr) && !unusualDaysStr.equals("null")) {
                                    engineeringDailyFeedBackBean.setUnusualDays(Integer.parseInt(unusualDaysStr));
                                }
                                engineeringDailyFeedBackBean.setUnusualReason(engineeringDailyFeedbackListJsonObj.getString("unusualReason"));
                                String unusualStatusStr = engineeringDailyFeedbackListJsonObj.getString("unusualStatus");
                                if (!TextUtils.isEmpty(unusualStatusStr)) {
                                    if (unusualStatusStr.equals("WAIT_CONFIRM")) {
                                        engineeringDailyFeedBackBean.setUnusualStatus(CommonEnum.UnusualStatus.WAIT_CONFIRM);
                                    } else if (unusualStatusStr.equals("WAIT_REPLY")) {
                                        engineeringDailyFeedBackBean.setUnusualStatus(CommonEnum.UnusualStatus.WAIT_REPLY);
                                    } else if (unusualStatusStr.equals("WAIT_REPLY_CONFIRM")) {
                                        engineeringDailyFeedBackBean.setUnusualStatus(CommonEnum.UnusualStatus.WAIT_REPLY_CONFIRM);
                                    } else if (unusualStatusStr.equals("REPLY_CONFIRM_FINISH")) {
                                        engineeringDailyFeedBackBean.setUnusualStatus(CommonEnum.UnusualStatus.REPLY_CONFIRM_FINISH);
                                    }

                                }


                                feedBackDetailListener.feedBackDetailsuccessed(engineeringDailyFeedBackBean);

                            }


                        } else {
                            String error_str = code;
                            error_str += "," + ConstantUtil.getErrorCode(code);
                            if (!json.isNull("error_str")) {
                                error_str += "," + json.getString("error_str");
                            }


                            feedBackDetailListener.feedBackDetailfailed(error_str);

                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {

                }
            }
        }).start();
    }

    public interface OnFeedBackDetailListener {
        void feedBackDetailsuccessed(EngineeringDailyFeedBackBean engineeringDailyFeedBackBean);

        void feedBackDetailfailed(String desc);
    }
}
